import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { CheckPlanApi, DictApi } from '#/api';

import { reactive, ref } from 'vue';

import { DictResp, getAutoCode } from '#/api';

const switchValue = ref('');

const mes_cycle_type = reactive<DictApi.SelectOption[]>([]);
const mes_order_status = reactive<DictApi.SelectOption[]>([]);
const dv_plan_type = reactive<DictApi.SelectOption[]>([]);

DictResp(['mes_cycle_type', 'mes_order_status', 'dv_plan_type']).then((res) => {
  // 将两个数组合并
  if (res.mes_cycle_type) {
    mes_cycle_type.push(...res.mes_cycle_type);
  }
  if (res.mes_order_status) {
    mes_order_status.push(...res.mes_order_status);
  }
  if (res.dv_plan_type) {
    dv_plan_type.push(...res.dv_plan_type);
  }
});

export function useSearchForm(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入计划编码',
        class: 'mr-5',
      },
      fieldName: 'planCode',
      label: '计划编码',
      labelClass: 'text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入计划名称',
      },
      fieldName: 'planName',
      label: '计划名称',
      labelClass: 'text-[#606266] font-bold',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: dv_plan_type,
        placeholder: '请选择类型',
      },
      fieldName: 'planType',
      label: '计划类型',
      labelClass: 'text-[#606266] font-bold',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: mes_order_status,
        placeholder: '请选择状态',
      },
      fieldName: 'status',
      label: '状态',
      labelClass: 'text-[#606266] font-bold',
    },
  ];
}

/**
 * 获取编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'planId',
      dependencies: {
        triggerFields: ['planId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'planCode',
      label: '计划编号',
      rules: 'required',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入计划编号',
      },
      dependencies: {
        triggerFields: ['switch'],
        trigger(values) {
          values.planCode = switchValue;
        },
      },
    },
    {
      component: 'Switch',
      fieldName: 'switch',
      label: '自动生成',
      formItemClass: 'col-span-1',
      componentProps: {
        defaultValue: 0,
        onChange: (e: boolean) => {
          if (e) {
            getAutoCode('CHECKPLAN_CODE').then((res) => {
              switchValue.value = res.data;
            });
          } else {
            switchValue.value = '';
          }
        },
      },
      dependencies: {
        triggerFields: ['planId'],
        show(values) {
          return !values.planId && true;
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'planName',
      label: '计划名称',
      componentProps: {
        placeholder: '请输入计划名称',
      },
      formItemClass: 'col-span-2 col-start-3',
    },
    {
      component: 'Select',
      rules: 'required',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: dv_plan_type,
        placeholder: '请选择类型',
        showSearch: true,
      },
      fieldName: 'planType',
      label: '计划类型',
      formItemClass: 'col-span-1 col-start-1',
    },
    {
      component: 'ElInputNumber',
      fieldName: 'cycleCount',
      formItemClass: 'col-span-1 col-start-3',
      label: '频率',
      rules: 'required',
      defaultValue: 1,
      componentProps: {
        min: 0,
        controls: true,
      },
    },
    {
      component: 'Select',
      rules: 'required',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: mes_cycle_type,
        placeholder: '请选择频率',
        showSearch: true,
      },
      labelClass: 'hidden',
      fieldName: 'cycleType',
      formItemClass: 'col-span-1',
    },
    {
      component: 'RangePicker',
      fieldName: 'rangePicker',
      label: '开始结束时间',
      formItemClass: 'col-span-2',
    },
    {
      component: 'Select',
      disabled: true,
      defaultValue: 'PREPARE',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: mes_order_status,
        placeholder: '请选择状态',
        showSearch: true,
      },
      label: '状态',
      fieldName: 'status',
      formItemClass: 'col-span-1 col-start-3',
    },
    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入标准',
        rows: 3,
        showCount: true,
      },
      fieldName: 'remark',
      label: '备注',
      emptyStateValue: null,
      formItemClass: 'col-span-4',
    },
  ];
}

/**
 * 获取表格列配置
 */
export function useColumns(
  onView: (param: CheckPlanApi.Row) => void,
): VxeTableGridOptions<CheckPlanApi.Row>['columns'] {
  return [
    {
      field: 'checkbox',
      fixed: 'left',
      type: 'checkbox',
      width: 50,
    },
    {
      field: 'planCode',
      title: '计划编码',
      width: 150,
      cellRender: { name: 'CellButtonInfo', props: { onClick: onView } },
    },
    {
      field: 'planName',
      title: '计划名称',
    },
    {
      field: 'planType',
      title: '计划类型',
      formatter: ({ cellValue }) => {
        const item = dv_plan_type.find((item) => item.value === cellValue);
        return item?.label || '无';
      },
    },

    {
      title: '开始日期',
      field: 'startDate',
    },
    {
      title: '结束日期',
      field: 'endDate',
    },
    {
      field: 'cycle',
      title: '频率',
      cellRender: {
        name: 'CellTopDown',
        props: {
          top: 'cycleCount',
          down: 'cycleType',
          downDict: mes_cycle_type,
        },
      },
    },
    {
      field: 'status',
      title: '状态',
      formatter: ({ cellValue }) => {
        const item = mes_order_status.find((item) => item.value === cellValue);
        return item?.label || '无';
      },
    },
    {
      title: '备注',
      field: 'remark',
    },
    {
      field: 'action',
      title: '操作',
      width: 150,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}
